home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / test / async1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  1.5 KB  |  76 lines

  1. /*
  2.  * Testing of new asynchronous portal interface.
  3.  * 
  4.  * Do the following at the monitor:
  5.  *
  6.  
  7.  create test1 (i = int4) \g
  8.  create test1a (i = int4) \g
  9.  
  10.  define rule r1 is on append to test1 do
  11.     [append test1a (i = new.i)
  12.     notify test1a]
  13.  
  14.  \g
  15.  * Then start up this process.
  16.  
  17.  append test1 (i = 10) \g
  18.  
  19.  * The value i=10 should be printed by this process.
  20.  */
  21.  
  22. #include "tmp/simplelists.h"
  23. #include "tmp/libpq.h"
  24.  
  25. void main()
  26. {
  27.     extern int PQAsyncNotifyWaiting;
  28.     PQNotifyList *l;
  29.     PortalBuffer *portalbuf;
  30.     char *res;
  31.     int ngroups,tupno, grpno, ntups, nflds;
  32.     PQsetdb(getenv("USER"));
  33.  
  34.     PQexec("listen test1a");
  35.  
  36.     while(1) {
  37.     sleep(1);
  38.     if (PQAsyncNotifyWaiting) {
  39.         PQexec(" ");
  40.         l = PQnotifies();
  41.         if (l != NULL) {
  42.         printf("notification on relation %s\n",l->relname);
  43.         res = PQexec("retrieve (test1a.i)");
  44.         if (*res == 'E') {
  45.             fprintf(stderr,"%s\nfailed",++res);
  46.             goto exit_error;
  47.         }
  48.         if (*res != 'P') {
  49.             fprintf(stderr,"%s\nno portal",++res);
  50.         }
  51.         /* get tuples in relation */
  52.         portalbuf = PQparray(++res);
  53.         ngroups = PQngroups(portalbuf);
  54.         for (grpno = 0; grpno < ngroups; grpno++) {
  55.             ntups = PQntuplesGroup(portalbuf, grpno);
  56.             if ((nflds = PQnfieldsGroup(portalbuf, grpno)) != 1) {
  57.             fprintf(stderr, "expected 1 attributes, got %d\n", nflds);
  58.             goto exit_error;
  59.             }
  60.             for (tupno = 0; tupno < ntups; tupno++) {
  61.             printf ("got i=%s\n",PQgetvalue(portalbuf,tupno,0));
  62.             }
  63.         }
  64.         break;
  65.         }
  66.     }
  67.     }
  68.  
  69.     PQfinish();
  70.     exit(0);
  71.   exit_error:
  72.     PQfinish();
  73.     exit(1);
  74.  
  75. }
  76.